﻿using UnityEngine;
using System.Collections;

public class GameMenu {

	public  UISprite uiBackground;
	private UIHorizontalLayout uiHorLayout;

	//Buttons
	private UIButton btnClose;
	private UIButton btnSave;
	private UIButton btnHome;
	
	private UISlider sliCtrlVol;
	
	private UIToolkit _tkMenu;
	
	public UIToolkit TkMenu
	{
		get { return _tkMenu;}
		set {_tkMenu = value;}
	}
	
	private UISprite spVolumen;
	
	private OptionMenuViewModel mvOption;
	
	public GameMenu(UIToolkit toolKit)
	{
		TkMenu = toolKit;
	}
	
	private void CreateControls()
	{
		
		uiBackground = TkMenu.addSprite("FondoMenu2b.png",0,0,2);
		GetBtnClose().parentUIObject = uiBackground;
		GetSpVolumen().parentUIObject= uiBackground;
		GetBtnHome().parentUIObject = uiBackground;
		GetsliCtrlVol().parentUIObject = uiBackground;
		GetBtnSave().parentUIObject = uiBackground;
		mvOption = new OptionMenuViewModel();
	}
	
	private void PositionControls()
	{
		uiBackground.positionCenter();
		GetBtnClose().positionFromTopRight(0.0f,0.0f);
		GetSpVolumen().positionFromTopLeft(0.2f,0.1f);
		GetsliCtrlVol().positionFromTopLeft(0.3f,0.3f);
		GetsliCtrlVol().refreshAnchorInformation();
		GetsliCtrlVol().centerize();
		GetBtnHome().positionFromBottomRight(0.1f,0.15f);
		GetBtnSave().positionFromBottomLeft(0.1f,0.2f);
	}
	
	private void ResizeControls()
	{
		//uiBackground.setSize(Screen.width,Screen.height);
	}
	
	private void PutActions()
	{
		GetBtnClose().onTouchUp += CloseMenu;
		GetsliCtrlVol().onChange += mvOption.ChangeVolume;
		GetBtnHome().onTouchUp += mvOption.Home;
		GetBtnSave().onTouchUp += mvOption.Save;
	}
	
	
	private UIButton GetBtnClose()
	{
		if(btnClose == null){
			btnClose = UIButton.create(TkMenu,"ExitMenuSmall.png","ExitMenuSmall.png",0,0);
		}
		return btnClose;
	}
	
	private UIButton GetBtnHome()
	{
		if(btnHome == null)
			btnHome = UIButton.create(TkMenu,"Home.png","Home.png",0,0);
		return btnHome;
	}
	
	private UIButton GetBtnSave()
	{
		if(btnSave == null)
			btnSave = UIButton.create(TkMenu,"Save.png","Save.png",0,0);
		return btnSave;
	}
	
	private UISlider GetsliCtrlVol()
	{
		if(sliCtrlVol == null)
		{
			sliCtrlVol= UISlider.create(TkMenu,"Knock.png", "SliderFondo.png", 0, 0, UISliderLayout.Horizontal );
			sliCtrlVol.value = 1.0f;
		}
		return sliCtrlVol;
	}
	
	private UISprite GetSpVolumen()
	{
		if(spVolumen == null)
		{
			spVolumen = new UISprite();
			spVolumen= TkMenu.addSprite("Song.png",0,0,0);
		}
		return spVolumen;
	}
	
	public void Show()
	{
		if(uiBackground == null)
			CreateControls();
		else
			OpenMenu();	
		PositionControls();
		PutActions();
		
	}
	
	public bool GetStateShow()
	{
		if(uiBackground != null)
			return !uiBackground.hidden;
		return false;
	}
	
	public void OpenMenu()
	{
		GetBtnClose().hidden = false;
		GetSpVolumen().hidden = false;
		GetsliCtrlVol().hidden = false;
		GetBtnHome().hidden = false;
		GetBtnSave().hidden = false;
		uiBackground.hidden = false;
	}
	
	public void CloseMenu(UIButton sender)
	{
		GetBtnClose().hidden = true;
		GetSpVolumen().hidden = true;
		GetsliCtrlVol().hidden = true;
		GetBtnHome().hidden = true;
		GetBtnSave().hidden = true;
		uiBackground.hidden = true;
	}
	
}
